<?php

class DefaultController extends Controller
{
   //public $layout='//layouts/column3';
   public $layout='//layouts/cms/main';
	public function actionIndex()
	{
	   
	   $user_id = Yii::app()->user->id;
      if(is_null($user_id))
         $this->redirect('/admin/default/login');
      else{
         $fields = array("MemberGroupID");
         $info = UserIdentity::getInfo($user_id,$fields);
         $memberGroupID = $info['MemberGroupID'];
         $model = new Menuadmin();
         $list = $model->findAllBySql("SELECT
                                       	menuadmin.*
                                       FROM
                                       	menuadmin
                                       INNER JOIN role ON menuadmin.MenuAdminID = role.MenuAdminID
                                       WHERE
                                       	menuadmin.IsActive = 1
                                       AND menuadmin.IsHome = 1
                                       AND role.MemberGroupID = $memberGroupID
                                       GROUP BY
                                       	menuadmin.MenuAdminID
                                       ORDER BY
                                       	menuadmin.MenuAdminID ASC");
          
         $menuIsHome = array();
         foreach($list as $object){
            $menuIsHome[] = $object->attributes;
         }
         
         $this->render('index',array('model' => $model,'menuIsHome' => $menuIsHome));
      }
      
	}
   
   /**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login()){
			   Yii::app()->session['admin_id'] = Yii::app()->user->id;
			   $this->redirect('/admin');
			}
				
		}

		$this->layout = '//layouts/cms/login';
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{  
		Yii::app()->user->logout();
		$this->redirect('/admin/default/login');
	}
}